package com.myspace_1.myproject_1;

import javax.swing.tree.TreeNode;
import java.util.*;

public class Student implements java.io.Serializable {

    static final long serialVersionUID = 1L;

    private java.lang.Integer id;

    private java.lang.String name;

    private int age;
    private  A a;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        if (id != null ? !id.equals(student.id) : student.id != null) return false;
        if (name != null ? !name.equals(student.name) : student.name != null) return false;
        if (a != null ? !a.equals(student.a) : student.a != null) return false;
        return address != null ? address.equals(student.address) : student.address == null;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + age;
        result = 31 * result + (a != null ? a.hashCode() : 0);
        result = 31 * result + (address != null ? address.hashCode() : 0);
        return result;
    }

    private java.lang.String address;

    public Student() {
    }

    public java.lang.Integer getId() {
        return this.id;
    }

    public void setId(java.lang.Integer id) {
        this.id = id;
    }

    public java.lang.String getName() {
        return this.name;
    }

    public void setName(java.lang.String name) {
        this.name = name;
    }



    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public java.lang.String getAddress() {
        return this.address;
    }

    public void setAddress(java.lang.String address) {
        this.address = address;
    }

    public Student(java.lang.Integer id, java.lang.String name, int age,
                   java.lang.String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    public int sumOfLeftLeaves(TreeNode root) {
        return sumOfLeft(root);
    }


    private  int sumOfLeft(TreeNode node){
        if(node==null||(node.left==null&&node.right==null)){
            return 0;
        }
        int leftCount=sumOfLeft(node.left);
        if(node.left!=null&&node.left.left==null&&node.left.right==null){
            leftCount=node.left.val;
        }
        int rightCount=sumOfLeft(node.right);
        return leftCount+rightCount;

    }

//    public int findBottomLeftValue(TreeNode root) {
//        Queue<TreeNode> queue=new LinkedList<>();
//        Queue<TreeNode> result=new LinkedList<>();
//        queue.add(root);
//        int size=queue.size();
//        while (size>0){
//            for (int i=size;i>0;i--){
//                TreeNode poll = queue.poll();
//                result.add(poll);
//                if(poll.left!=null){
//                    queue.add(poll.left);
//                }
//                if(poll.right!=null){
//                    queue.add(poll.right);
//                }
//            }
//            size=queue.size();
//            if(size!=0){
//                result=new LinkedList<>();
//            }
//        }
//        return result.poll().val;
//    }

    int depthd=Integer.MIN_VALUE;
    int result=0;
    public int findBottomLeftValue(TreeNode root) {
         findBottom(root,0);
         return result;

    }
    private void findBottom(TreeNode node,int depth){
        if(node.left==null&&node.right==null){
            if(depth>depthd){
                result=node.val;
                depthd=depth;
            }
        }
        if(node.left!=null){
            findBottom(node.left,depth+1);
        }
        if(node.right!=null){
            findBottom(node.right,depth+1);
        }
    }

    public boolean hasPathSum(TreeNode root, int targetSum) {
        return hasPath(root,targetSum,0);
    }

    private boolean hasPath(TreeNode node,int targetSum,int nowSum){
        if(node==null){
            return false;
        }
        if(node.left==null&&node.right==null){
            if(node.val+nowSum==targetSum){
                return true;
            }else {
                return false;
            }
        }
        boolean leftResult=false;
        if(node.left!=null){
            leftResult= hasPath(node.left,targetSum,nowSum+node.val);
        }
        if(leftResult){
            return true;
        }
        boolean rightResult=false;
        if(node.right!=null){
            rightResult= hasPath(node.right,targetSum,nowSum+node.val);
        }
        if(rightResult){
            return true;
        }
        return false;
    }

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> result=new ArrayList<>();
        path(root,targetSum,result,new ArrayList<>());
        return result;

    }

    private void path(TreeNode node,int targetSum,List<List<Integer>> result,List<Integer> temp){
        if(node==null){
            return;
        }
        if(node.left==null&&node.right==null){
            if(targetSum==node.val){
                temp.add(node.val);
                result.add(new ArrayList<>(temp));
            }
            return;
        }
        if(node.left!=null){
            temp.add(node.val);
            path(node.left,targetSum-node.val,result,temp);
            temp.remove(temp.size()-1);
        }
        if(node.right!=null){
            temp.add(node.val);
            path(node.right,targetSum-node.val,result,temp);
            temp.remove(temp.size()-1);
        }
    }

}
